package cn.itcast.bos.web.action.common;




import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;



public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
	protected T model;

	@Override
	public T getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	public BaseAction() {
		//this指的是继承BaseAction的类,得到带有泛型的父类型--BaseAction<T>
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		//获取第一个泛型参数
		ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
		Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		try {
			model= modelClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("模型构造失败");
		}
	}
	
	
	protected int page;
	protected int rows;
	
	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	} 
	
	public void pushPageDataToValueStack(Page<T> pages){
		System.out.println(pages);
		Map<String, Object> map=new HashMap<>();
		map.put("total", pages.getTotalElements());
		map.put("rows",pages.getContent());
		ActionContext.getContext().getValueStack().push(map);
	}
	

}
